﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RedHouse.Data.Entities;
using RedHouse.Data.Service;
using RedHouse.Data.Base;
using RedHouse.Data.Common;
using RedHouse.Web.common;
using RedHouse.Data;

namespace RedHouse.Web
{
    public partial class Menu : PageBase
    {
        public List<RedHouseCategory> Menus { get; set; }
        private ICategoryService _categoryService;
        private ILanguageService _languageService;
        protected void Page_Load(object sender, EventArgs e)
        {
            Initialize();
            if (Request.QueryString["id"] != null)
            {
                Guid menuId = new Guid(Request.QueryString["id"].ToString());
                UCDetail.MenuDetail = Menus.Where(m => m.Id == menuId).FirstOrDefault();
                UCDetail.MenuId = menuId;
                UCDetail.Initialize();

                UCDetail.Visible = true;
                rptMenu.Visible = false;
            }
        }

        private void Initialize()
        {
            _categoryService = new CategoryService();
            _languageService = new LanguageService();

            Menus = _categoryService.GetCategoryByGroup(RedHouseGroup.GroupList[4].Value);
            if (Session["location"] != null)
            {
                Menus = Menus.Where(m => m.LocationId == int.Parse(Session["location"].ToString()) || m.LocationId==3).ToList();
            }

            Language language;
            if (Session["Culture"] != null && Session["Culture"].ToString() == "en-US")
            {
                language = _languageService.GetAllLanguage().Where(l => l.Description == "English").FirstOrDefault();
            }
            else
            {
                language = _languageService.GetAllLanguage().Where(l => l.Description != "English").FirstOrDefault();
            }

            if (language != null)
            {
                Menus = Menus.Where(d => d.LangId == language.ID).ToList();
            }

            Menus = Menus.OrderBy(m => m.DisplayOrder).ToList();
            rptMenu.DataSource = Menus;
            //rptMenu.DataBind();
            UCDetail.Visible = false;
            rptMenu.Visible = true;
        }
    }
}